package com.scommunity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.teautil.models.RuntimeOptions;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.scommunity.conf.AliConf;
import com.scommunity.service.ShortMessageService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ShortMessageServiceImpl implements ShortMessageService {

    @Value("${ali.sms.accessKeyId}")
    private String accessKeyId;
    @Value("${ali.sms.accessKeySecret}")
    private String accessKeySecret;
    @Value("${ali.sms.signName}")
    private String signName;
    @Value("${ali.sms.templateCode}")
    private String templateCode;

    @Autowired
    private Client client;


    @Override
    public boolean send(Map<String, Object> map, String phone) {
        if (phone != null && "".equals(phone)) return false;

        DefaultProfile profile =
                DefaultProfile.getProfile("default", accessKeyId,
                        accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        request.putQueryParameter("PhoneNumbers", phone);   //手机号
        request.putQueryParameter("SignName", signName);    //签名名称
        request.putQueryParameter("TemplateCode", templateCode);  //模板名称
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));  //验证码转换json数据

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @SneakyThrows
    @Override
    public boolean sendSms(String phone, String code) {
        int code1 =  Integer.parseInt(code);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\""+code+"\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        SendSmsResponse smsResponse = client.sendSmsWithOptions(sendSmsRequest,runtime);
        SendSmsResponseBody body = smsResponse.getBody();
        System.out.println(body.toString());
        String bodyCode = body.getCode();
        if ("OK".equals(bodyCode)){
            return true;
        }
        return false;
    }
}
